﻿using System;
using System.Collections.Generic;
using System.Text;

namespace iPdf
{
    class DecryptOption : SecurityOption
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public bool KeepOpenPassword;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public bool ChangePermissions;
    }

    class DecryptPage : SecurityPage
    {
        protected override PageOption PageOption
        {
            get
            {
                DecryptOption option = new DecryptOption();
                
                //set security level
                SecurityLevel securityLevel = (SecurityLevel)EncryptMethod.SelectedItem;
                option.SecurityLevelId = securityLevel.ID;

                //set certificate
                option.Certificate = Certificate.DefaultCertificate;
                option.Certificate.Password = CertificatePassword.Text;

                //set passwords
                option.OwnerPassword = OwnerPassword.Text;

                option.KeepOpenPassword = KeepOpenPassword.Checked;
                option.ChangePermissions = ChangePermissions.Checked;
                
                //set permissions
                option.Permission = this.Permission;

                return option;
            }
        }

        public override string IntroInfo
        {
            get
            {
                return global::iPdf.Properties.Resources.DecryptPageIntroInfo;
            }
        }

        public DecryptPage()
            : base()
        {
            actor = new PdfDecryptor();
        }

        public override void UpdatePageOnlyControls()
        {
            base.UpdatePageOnlyControls();

            if(Global.MainWindow.Busy || !Global.Grid.HasItems)
            {
                return;
            }

            bool hasOwnerPassword = OwnerPassword.Enabled && !string.IsNullOrEmpty(OwnerPassword.Text);

            ChangePermissions.Enabled = ReadyToAct &&
                                                !(OwnerPassword.Enabled && string.IsNullOrEmpty(OwnerPassword.Text));
            ChangePermissions.Checked = ChangePermissions.Enabled ?
                                                        ChangePermissions.Checked : false;

            KeepOpenPassword.Enabled = hasOwnerPassword && ChangePermissions.Checked;
            KeepOpenPassword.Checked = KeepOpenPassword.Enabled ? 
                KeepOpenPassword.Checked : false;

            Helper.EnableControl(PermissionsGroup, ChangePermissions.Checked);
            ScreenReaderPermission.Enabled = !CopyContentPermission.Checked;
        }

        protected override void LoadCertificateSubgroup()
        {
            base.LoadCertificateSubgroup();
            int controlsCount = this.CryptoGroup.Controls.Count;
            this.CryptoGroup.Controls.SetChildIndex(Certificate, controlsCount - 3);
            this.CryptoGroup.Controls.SetChildIndex(CertificatePassword, controlsCount - 2);
        }

        private void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePageOnlyControls();
        }

        public override void ImportSession(System.Xml.XmlElement sessionNode)
        {
            base.ImportSession(sessionNode);

            SetCheckBoxBySession(sessionNode, ChangePermissions);
            SetCheckBoxBySession(sessionNode, KeepOpenPassword); 
        }

        public override void ExportSession(System.Xml.XmlElement sessionNode)
        {
            base.ExportSession(sessionNode);

            SetXmlElementAttribute(sessionNode, KeepOpenPassword.Name, 
                KeepOpenPassword.Checked.ToString());
            SetXmlElementAttribute(sessionNode, ChangePermissions.Name,
                ChangePermissions.Checked.ToString());
        }

        protected override void InitializeComponent()
        {
            base.InitializeComponent();

            this.KeepOpenPassword = new Elegant.Ui.CheckBox();
            this.ChangePermissions = new Elegant.Ui.CheckBox();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.CryptoGroup)).BeginInit();
            this.CryptoGroup.SuspendLayout();

            this.CryptoGroup.Controls.Add(this.KeepOpenPassword);
            this.CryptoGroup.Controls.Add(this.ChangePermissions);
            // 
            // KeepOpenPassword
            // 
            this.KeepOpenPassword.Id = "9ef620c8-5ad3-4517-8df7-3bf33856490c";
            this.KeepOpenPassword.Location = new System.Drawing.Point(430, 2);
            this.KeepOpenPassword.Name = "KeepOpenPassword";
            this.KeepOpenPassword.Size = new System.Drawing.Size(176, 24);
            this.KeepOpenPassword.TabIndex = 6;
            this.KeepOpenPassword.Text = "Keep Open Password If Exists";
            this.KeepOpenPassword.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);

            // 
            // ChangePermissions
            // 
            this.ChangePermissions.Id = "9ef620c8-5ad3-4517-8df7-3bf33856490c";
            this.ChangePermissions.Location = new System.Drawing.Point(430, 2);
            this.ChangePermissions.Name = "RemoveOpenPasswordOnly";
            this.ChangePermissions.Size = new System.Drawing.Size(176, 24);
            this.ChangePermissions.TabIndex = 6;
            this.ChangePermissions.Text = "Change Permissions";
            this.ChangePermissions.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);

            ((System.ComponentModel.ISupportInitialize)(this.CryptoGroup)).EndInit();
            this.CryptoGroup.ResumeLayout(false);
            this.CryptoGroup.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #region Fields
        private Elegant.Ui.CheckBox KeepOpenPassword;
        private Elegant.Ui.CheckBox ChangePermissions;
        #endregion
    }
}
